package com.hyl.crmclient.config.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/6/13 8:54
 **/
public class ShiroTest {

	@Test
	public void testShiro(){
		//1、创建realm
		IniRealm realm = new IniRealm("classpath:shiro.ini");
		//2、创建SecurityManger
		DefaultSecurityManager securityManager = new DefaultSecurityManager();
		securityManager.setRealm(realm);
		SecurityUtils.setSecurityManager(securityManager);
		//3、subject
		Subject subject = SecurityUtils.getSubject();
		//token
		UsernamePasswordToken token = new UsernamePasswordToken(
				"admin", "123456");
		try {
			//认证
			subject.login(token);
		}catch (AuthenticationException e){
			e.printStackTrace();
		}
		System.out.println("是否认证成功："+subject.isAuthenticated());
		System.out.println("身份："+subject.getPrincipal());

		//判断角色
		System.out.println("角色："+subject.hasRole("管理员"));
		//判断权限
		System.out.println("权限："+subject.isPermitted("chance:del"));
	}
}
